package com.lottery.controller;

import com.lottery.dao.UpPlanDao;
import com.lottery.entity.Lottery;
import com.lottery.entity.Query;
import com.lottery.entity.UpPlan;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by c0de8ug on 16-2-14.
 */
@Controller
public class LoginController {

	Logger logger = LoggerFactory.getLogger(LoginController.class);

	@Autowired
	private Mainserver mainserver ;
	@Autowired
	private UpPlanDao upPlanDao;

	@RequestMapping("login")
	public String login(HttpServletRequest req,HttpServletResponse resp, Model model, HttpSession session, Query query) {

		// TODO
		// 这里以后可以把角色更换成资源控制后动态生成页面,（-->这里有疑问-->是不是可以使用自定义角色？shiro张开涛的16章有个自定义标签扫描出的角色）
		org.apache.shiro.subject.Subject subject = SecurityUtils.getSubject();
		boolean isAuthenticated = subject.isAuthenticated();

		if (isAuthenticated) {
			String principal = (String) subject.getPrincipal();
			logger.info("******************************principal**************************" + principal);
			query.setMemberAccount(principal);

			Date d = new Date();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if (StringUtils.isBlank(query.getBeginDate())) {
				query.setBeginDate(sdf.format(d));
			}
            query.setProvince("广东");
            query.setPlay("任2");
            
            Lottery new1 = parseLottery(upPlanDao.getNewList(query));
			List<Lottery> lotteries1 =  mainserver.getLotteryList(query);
            query.setProvince("江苏");
            query.setPlay("任2");
            Lottery new2 = parseLottery(upPlanDao.getNewList(query));
            List<Lottery> lotteries2 =  mainserver.getLotteryList(query);
            query.setProvince("山东");
            query.setPlay("任2");
            Lottery new3 = parseLottery(upPlanDao.getNewList(query));
            List<Lottery> lotteries3 =  mainserver.getLotteryList(query);
            query.setProvince("广东");
            query.setPlay("任3");
            Lottery new4 = parseLottery(upPlanDao.getNewList(query));
            List<Lottery> lotteries4 =  mainserver.getLotteryList(query);
            query.setProvince("江苏");
            query.setPlay("任3");
            Lottery new5 = parseLottery(upPlanDao.getNewList(query));
            List<Lottery> lotteries5 =  mainserver.getLotteryList(query);
            query.setProvince("山东");
            query.setPlay("任3");
            Lottery new6 = parseLottery(upPlanDao.getNewList(query));
            List<Lottery> lotteries6 =  mainserver.getLotteryList(query);
            query.setProvince("广东");
            query.setPlay("任5");
            Lottery new7 = parseLottery(upPlanDao.getNewList(query));
            List<Lottery> lotteries7 =  mainserver.getLotteryList(query);
            query.setProvince("江苏");
            query.setPlay("任5");
            Lottery new8 = parseLottery(upPlanDao.getNewList(query));
            List<Lottery> lotteries8 =  mainserver.getLotteryList(query);
            query.setProvince("山东");
            query.setPlay("任5");
            Lottery new9 = parseLottery(upPlanDao.getNewList(query));
            List<Lottery> lotteries9 =  mainserver.getLotteryList(query);
			session.setAttribute("username", principal);
			session.setAttribute("lotteries1", lotteries1);
            session.setAttribute("lotteries2", lotteries2);
            session.setAttribute("lotteries3", lotteries3);
            session.setAttribute("lotteries4", lotteries4);
            session.setAttribute("lotteries5", lotteries5);
            session.setAttribute("lotteries6", lotteries6);
            session.setAttribute("lotteries7", lotteries7);
            session.setAttribute("lotteries8", lotteries8);
            session.setAttribute("lotteries9", lotteries9);
            session.setAttribute("new1", new1);
            session.setAttribute("new2", new2);
            session.setAttribute("new3", new3);
            session.setAttribute("new4", new4);
            session.setAttribute("new5", new5);
            session.setAttribute("new6", new6);
            session.setAttribute("new7", new7);
            session.setAttribute("new8", new8);
            session.setAttribute("new9", new9);
            return "/main";

		}
		model.addAttribute("errorLoginMessage", "用户名或密码错误");
		return "login";
	}

	
	private Lottery parseLottery(List<UpPlan> list){
		Lottery l = new Lottery();
		if(!CollectionUtils.isEmpty(list)){
			for(UpPlan up : list){
				logger.info(up.toString());
			}
			String p = list.get(0).getPeriods();
        	for(UpPlan up : list){
        		if(p.equals(up.getPeriods())){
        			if(up.getScheme().equals("1")){
            			l.setScheme(up.getScheme());
            			l.setNumber1(up.getNumber1());
            			l.setNumber2(up.getNumber2());
            			l.setNumber3(up.getNumber3());
            		}
            		if(up.getScheme().equals("2")){
            			l.setScheme(up.getScheme());
            			l.setNumber1Two(up.getNumber1());
            			l.setNumber2Two(up.getNumber2());
            			l.setNumber3Two(up.getNumber3());
            		}
            		if(up.getScheme().equals("3")){
            			l.setScheme(up.getScheme());
            			l.setNumber1Three(up.getNumber1());
            			l.setNumber2Three(up.getNumber2());
            			l.setNumber3Three(up.getNumber3());
            		}
        		}
        	}
        }
		return l;
	}

}
